<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

#载入model
load_model(array("vote"));

$id = intval($_GET['id']);
$rs = get_vote_by_id($id);
if(empty($rs['id'])){
	exit("No Record!");
}

#增加views
add_views($id);

#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = htmlspecialchars($rs['title']) . " - Votings";
#用于获取针对本页的css样式
$css = array();
#用于获取针对本页的js
$js = array("jquery.masonry.min");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "vote";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
$(function(){
    //$("#vote-after").hide();

	var $container = $('#container');
	
	$container.imagesLoaded( function(){
	  $container.masonry({
	    itemSelector : '.pin',
	    isAnimated: 'true'
	  });
	});
	
	$("#comment-btn").click(function(){
	    var msgbody = $("#comment-from").val();
		if(msgbody.length <= 4){
			alert("请输入大于4个字符！");
			return false;
		}	
			$.ajax({
				type: "POST",
				url: "/ajax/comment.php",
				data: "action=add&vote_id=<?php echo $id ?>&msgbody="+msgbody,
				success: function(msg){
					if(msg==1){
						alert("请先登录！");
						location.href='/login.php';
					}else{
						$(".box-header h2").html("评论");
						$("#comment").html(msg);
						//location.reload(true);
					}
				}
			});
	});	
	
		$(".voting-spam").click(function(){
			$.ajax({
				type: "GET",
                url: "/ajax/vote.php?vote_id=<?php echo $id?>&action=rabbish",
				success: function(msg){
					if(msg==1){
						alert("请先登录！");
						location.href='/login.php';
					}else{
						$(".voting-spam").hide();
					}
				}
			});
	});	
	
		$(".voting-close").click(function(){
			$.ajax({
				type: "GET",
                url: "/ajax/vote.php?vote_id=<?php echo $id;?>&vuser=<?php echo $rs['user_id'];?>&action=close",
				success: function(msg){
					if(msg==1){
						alert("请先登录！");
						location.href='/login.php';
					}else{
						$(".voting-close").parent().hide();
						$("#vote-before").hide();	
						$("#vote-after").hide();
					}
				}
			});
	});
	
	<?php
	#//查找用户的投票记录
	#已登录用户
	if(is_loggedin()){
	$u = get_user_vote($id);
	if(isset($u['value'])){
		if($u['value'] == 1) {
	?>
		$("#vote-after p").html("您选择了“Yes”票。感谢参与！");
	<?php
		}else if($u['value'] == 0){
	?>	
		$("#vote-after p").html("您选择了“No”票。感谢参与！");
	<?php
		}
	}
	}
	?>
});

function deletes(obj, msg_id) {
	if (confirm("确定删除评论?")) {
		$.ajax({
			type: "POST",
			url: "/ajax/comment.php",
			data: "action=delete&msg_id="+msg_id,
			success: function(msg){
			if(msg==1){
					alert("请先登录！");
					location.href='/login.php';
				}else{
					$(obj).parent().parent().remove();
				}
			}
		});
	}
} 

function getpage(page)
{
	$.ajax({
	   type: "GET",
	   url: "/ajax/page_comment.php?vote_id=<?php echo $id?>&page="+page,
	   success: function(msg){
		   if(msg!==''){
				$("#comment").html(msg);
		   }
	   }
	});
}

function add()
{
	$.ajax({
	   type: "GET",
	   url: "/ajax/vote.php?vote_id=<?php echo $id?>&action=add",
	   success: function(msg){
		   if(msg == 1){
				alert("请先登录！");
				location.href='/login.php';
				return false;
			}else if(msg == 2){
				//first
				$("#votings-up").html(parseInt($("#votings-up").text())+1);
			}else if(msg == 3){
				$("#votings-up").html(parseInt($("#votings-up").text())+1);
				$("#votings-down").html(parseInt($("#votings-down").text())-1);
				//second
			}
		$("#vote-before").hide();	
		$("#vote-after").show();
		$("#vote-after p").html("您选择了“Yes”票。感谢参与！");
	   }
	});
}

function sub()
{
	$.ajax({
	   type: "GET",
	   url:  "/ajax/vote.php?vote_id=<?php echo $id?>&action=sub",
	   success: function(msg){
		   if(msg == 1){
				alert("请先登录！");
				location.href='/login.php';
				return false;
			}else if(msg == 2){
				//first
				$("#votings-down").html(parseInt($("#votings-down").text())+1);
			}else if(msg == 3){
				$("#votings-down").html(parseInt($("#votings-down").text())+1);
				$("#votings-up").html(parseInt($("#votings-up").text())-1);
				//second
			}
			$("#vote-before").hide();	
			$("#vote-after").show();
			$("#vote-after p").html("您选择了“No”票。感谢参与！");
	   }
	});
}

function change()
{
	$("#vote-before").show();	
	$("#vote-after").hide();
}
</script>
<div id="main">
	<div class="box">
		<div class="voting-top clearfix">
			<div class="user-info">
				<div class="name"><?php echo $rs['name'];?></div>
				<div class="tips">发布于 <?php echo get_diff_date($rs['create_time']) ?></div>
				<a href="<?php echo _CONST_WEB_URL . 'homepage.php?id='.$rs['user_id'];?>"><img class="header-pic" src="<?php echo iif(!empty($rs['avatar']), _CONST_PIC_URL . 'avatar/' . $rs['user_id'] . '/2_' . $rs['avatar'], _CONST_IMG_URL . 'avatar/2.jpg')?>" alt="<?php echo $rs['name'] ?>" /></a>
			</div>
			<div class="voting-sharing">
				<span class="sharing-title">分享这个投票：</span>
				<span><a href="#sina" class="lnk-sina" title="分享到新浪微博">分享到新浪微博</a><a href="#renren" class="lnk-renren" title="分享到人人网">分享到人人网</a><a href="#douban" class="lnk-douban" title="分享到豆瓣">分享到豆瓣</a><a href="#msn" class="lnk-msn" title="分享到MSN">分享到MSN</a><a href="#kx" class="lnk-kx" title="分享到开心网">分享到开心网</a></span>
			</div>
		</div>
		<div class="voting-header">
			<h1><?php echo htmlspecialchars($rs['title'])?></h1>
			<div class="votings-data"><span class="votings-down" id="votings-down"><?php echo $rs['sub']?></span><span class="votings-up" id="votings-up"><?php echo $rs['add']?></span><span class="votings-views"><?php echo $rs['views']?></span></div>
		</div>
		<div class="voting-body">
			<div class="voting-pics">
				<div class="user-pic"><img src="<?php echo _CONST_PIC_URL . 'thumb/' . $rs['user_id'] . '/2_' .$rs['thumbfile']?>" alt="<?php echo $rs['name']?>" /><a href="<?php echo _CONST_PIC_URL . 'thumb/' . $rs['user_id'] . '/' .$rs['thumbfile']?>" target="_blank" class="user-pic-zoom">查看原图</a></div>
				<div class="product-pic"><img src="<?php echo _CONST_PIC_URL . 'vote/' . $rs['user_id'] . '/2_' .$rs['images']?>" alt="<?php echo $rs['title']?>" /><a href="<?php echo _CONST_PIC_URL . 'vote/' . $rs['user_id'] . '/' .$rs['images']?>" target="_blank" class="product-pic-zoom">查看原图</a></div>
				<div class="voting-link"><img src="media/bg/voting-link.png" alt="voting" /></div>
			</div>
			<div class="voting-more clearfix">
				<blockquote class="voting-des"><?php echo htmlspecialchars($rs['desc'])?></blockquote>
				<div class="voting-buy-url">
					<a href="<?php echo $rs['url']?>" class="view-more" target="_blank">查看更多商品信息</a>
				</div>
			</div>
		</div>
		<?php 
		if($rs['lock']!=1){
		?>
		<div class="voting-foot" id="vote-before" <?php echo iif(isset($u['value']),"style='display:none;'");?>>
			<div class="voting-txt">是否适合我？</div>
			<div class="voting-yes">
				<a class="button" href="javascript:add();"></a>
			</div>
			<div class="voting-no">
				<a class="button" href="javascript:sub();"></a>
			</div>
		</div>
		<!--投完票切换下面的状态-->
		<div class="voting-foot" id="vote-after" <?php echo iif(!isset($u['value']),"style='display:none;'");?>><p>您选择了“Yes”票。感谢参与！</p><a href="javascript:change();" class="changed-mind">我改变了主意！</a></div>
		<?php
		if(is_loggedin()){
		?>
		<div class="voting-op clear fix"><a href="javascript:;" class="voting-spam">这是垃圾投票！</a><?php if($_SESSION['userid'] == $rs['user_id']){?><a href="javascript:;" class="voting-close">关闭投票！</a><?php }?></div>
		<?php 
		}
		}
		?>
		<!--voting end-->
		<?php
		#获得评论
		$t = get_commentcount_by_voteid($id);
		if($t>0){
		?>
		<!--上面是没有回复的样式，下面是有回复的样式-->
		<div class="box-header"><h2>评论</h2></div>
		<div id="comment">
		<?php 
			$rc = get_comment_by_voteid($id);
			foreach($rc as $c) {
		?>
		<div class="comment-list clearfix">
			<a href="<?php echo _CONST_WEB_URL . 'homepage.php?id='.$c['user_id'];?>"><img class="comment-user-image" src="<?php echo iif(!empty($c['avatar']), _CONST_PIC_URL . 'avatar/' . $c['user_id'] . '/2_' . $c['avatar'], _CONST_IMG_URL . 'avatar/2.jpg')?>" alt="<?php echo $c['postname'] ?>" /></a>
			<div class="comment-user-name"><a href="<?php echo _CONST_WEB_URL . 'homepage.php?id='.$c['user_id'];?>"><?php echo $c['postname'] ?></a><span class="comment-time"><?php echo date('Y-m-d H:i:s', $c['posttime']);?></span></div>
			<div class="reply-to-comment"><?php echo htmlspecialchars($c['postcontent'])?></div>	
			<?php
			if($_SESSION['buserid']>0 || $_SESSION['userid'] == $c['user_id']){
			?>
			<div class="comment-del"><a href="javascript:;" onclick="deletes(this,<?php echo $c['id'];?>);">删除</a></div>
		   <?php
			}		   
		   ?>
		</div>
		<?php
			}
		?>	
		<?php 
		if($t>5){
			pagination_comment_ajax(1, 5, $t, "page=%s%");
		}
		?>
		</div>
		<?php }else{
		?>
		<div class="box-header"><h2>目前还没有评论. 赶紧抢沙发!</h2></div>
		<div id="comment">
		</div>
		<?php
		}
		?>
		<!--回复开始-->
		<div class="input-wrap clearfix">
			<textarea class="comment-from" id="comment-from" onfocus="if(this.value=='在这里写下你的评论'){this.value='';}" onblur="if(this.value==''){this.value='在这里写下你的评论'}">在这里写下你的评论</textarea>
			<input type="button" name="comment-btn" id="comment-btn" class="comment-btn" value="" />
		</div>
		<!--回复结束-->
	</div>
	<!--hot votings-->
	<div class="box box-header"><h1>热门投票</h1></div>
	<?php
	$hv = get_hot_vote();
	if(sizeof($hv)>0){
	?>
	<div id="container">
	<?php 
	foreach($hv as $h){
	?>
		<div class="pin">
			<a href="<?php echo _CONST_WEB_URL . 'vote.php?id=' . $h['id']?>"><img src="<?php echo _CONST_PIC_URL . 'vote/' . $h['user_id'] . '/1_' .$h['images']?>" /></a>
			<div class="votings-info">
				<div class="votings-user"><span class="name"><?php echo $h['name']?></span><img src="<?php echo iif(!empty($h['avatar']), _CONST_PIC_URL . 'avatar/' . $h['user_id'] . '/1_' . $h['avatar'], _CONST_IMG_URL . 'avatar/1.jpg')?>" alt="<?php echo $h['name']?>" /></div>
				<div class="votings-data"><span class="votings-down"><?php echo $h['sub']?></span><span class="votings-up"><?php echo $h['add']?></span></div>
			</div>
		</div>
	<?php 
	}
	?>
	</div>
	<?php 
	}
	?>
	<!--hot votings end-->	
</div>
<!--main end-->
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
